/*
 * MIT License
 * 
 * Copyright (c) 2017 wen.gu <454727014@qq.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*****************************************************************************
 * Name:tiny_event_looper.h
 *
 * Purpose: event looper implementation
 *     
 * Version: 0.1
 *
 * Developer:
 *     wen.gu,2017-03-09
 *
 *
 *****************************************************************************/


#ifndef __TINY_EVENT_LOOPER_H__
#define __TINY_EVENT_LOOPER_H__

#include "tiny_event.h"

#ifdef __cplusplus
extern "C"{
#endif

/***************************************************************************
 *
 * macro declaration
 *
 ***************************************************************************/
#define TINY_INVALID_LOOPER_ID (-1)
/***************************************************************************
 *
 * data structure declaration
 *
 ***************************************************************************/

/** to indentify current looper */
typedef GS32 tiny_looper_id;



/** the callback function of user process event */
typedef void (*tfLooperOnEventFunc_t)(void* opaque, tiny_event_t* evt);


/***************************************************************************
 *
 * API declaration
 *
 ***************************************************************************/
/**
 *@brief  create a looper
 *
 *@param  opaque [in] pointer to a private data for the parameter of on event callbck function.
 *@param  on_evt_cb [in] callback function to reponse event
 *
 *@return success: looper instance handle, fail: NULL.
 *@see
 *
 */
G_API GPHD tfLooperCreate(void* opaque, tfLooperOnEventFunc_t on_evt_cb);

/**
 *@brief  destroy a looper instance 
 *
 *@param  looper [in] pointer to looper instance
 *
 *@return none.
 *@see
 *@note  when looper destroy, it will auto unregister from looper.
 *
 */
G_API void tfLooperDestroy(GPHD looper);

/**
 *@brief  create a looper
 *
 *@param  looper [in] pointer to looper instance
 *@param  name [in] the name of current looper(maxmim length: 31, else will be cut)
 *@param  priority [in] the priority of looper thread(@see  vos_thread.h)
 *
 *@return success: G_OK, fail: error code.
 *@see
 *
 */
G_API GS32 tfLooperStart(GPHD looper, const char* name, GS32 priority);

/**
 *@brief  post a event with delay time (async mode)
 *
 *@param  looper   [in] pointer to looper instance
 *@param  evt      [in] pointer to event data structure
 *
 *@return none.
 *@see
 *
 */
G_API void tfLooperPost(GPHD looper, tiny_event_t* evt);

/**
 *@brief  post a reply event(async mode)
 *
 *@param  looper   [in] pointer to looper instance
 *@param  evt      [in] pointer to event data structure
 *@param  reply_id [in] indicate which event need we reply.
 *
 *@return none.
 *@see
 *
 */
G_API void tfLooperReply(GPHD looper, tiny_event_t* evt, GU32 reply_id);

/**
 *@brief  post a event and wait reply(sync mode)
 *
 *@param  looper   [in] pointer to looper instance
 *@param  evt      [in] pointer to event data structure
 *@param  response [io] pointer to reply event data structure.
 *
 *@return success: G_OK, fail: error code.
 *@see
 *
 */
G_API GS32 tfLooperPostAndWaitResponse(GPHD looper, tiny_event_t* evt, tiny_event_t** response);


#ifdef __cplusplus
}
#endif

#endif //end of __TINY_EVENT_LOOPER_H__
